$(function () {
	refresh(document.getElementById("graphicCodeImg"));
	// 个人注册按钮
	$("#registerBtnWen").click(function (e) {
		e.preventDefault();
		checkForm();



	});

	$("#readAgreement").click(function () {
		if (document.getElementById("readAgreement").checked) {
			$("#readAgreement_msg_img").empty().append('<img width="18px"  src="' + themepath + 'images/successicon.jpg"/>');
			$("#readAgreement_msg").html("已阅读协议");
		} else {
			document.getElementById("readAgreement_msg").style.color = "red";
			$("#readAgreement_msg_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
			$("#readAgreement_msg").html("请选中协议");
		}
	});
});
//又一个注册页面添加校验
//$(function() {

//个人提交的验证表单
function checkForm() {
	//var custType = $("#custType").val();
	//检验密码
	var true_password = $("#true_password").val();
	if (true_password != "1") {
		document.getElementById("password_span").style.color = "red";
		$("#errMsg").html("请正确填写密码");
		$("#password").focus();
		$("#password_span_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
		return false;
	}

	//检验手机号码
	var true_telphone = $("#true_telphone").val();
	if (true_telphone != "1") {
		document.getElementById("telphone_span").style.color = "red";
		$("#errMsg").html("请正确填写手机号码");
		$("#telphone").focus();
		$("#telphone_span_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
		return false;
	}

	//检验手机验证码
	var true_phone_msg = $("#true_phone_msg").val();
	if (true_phone_msg != "1") {
		$("#errMsg").html("请正确填写手机验证码");
		$("#tel_checkCode").focus();
		$("#phone_msg_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
		return false;
	}


	//是否选中服务协议
	if (document.getElementById("readAgreement").checked) {
		$("#readAgreement").attr("checked", "checked");
	}
	if ($("#readAgreement").attr("checked") == "checked") {
		$("#readAgreement_msg_img").empty();
		$("#readAgreement_msg").html("");
	} else {
		//$("#readAgreement_msg_img").empty().append('<img width="18px"  src="'+themepath+'images/errorimg.png"/>');
		$("#errMsg").html("请选中宾和服务协议");
		//$("#readAgreement_msg").html("请选中协议");
		return false;
	}
	//第一次提交注册申请后，就禁用提交按钮、防止用户多次点击提交申请
	$("#registerBtnWen").attr("disabled", true).attr("color", "grey");
	$("#registerBtnWen").html("注册申请中...");
	$("#registerFormWen").submit();
}




// 个人注册检查 密码
function chkPass(obj) {
	$("#password_span_img").empty();
	$("#errMsg").html("");
	$("#password_span_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
	// 校验密码
	if (isEmpty(obj.value)) {
		$("#errMsg").html("密码不能为空");
		$("#tip-password").css("background", "");
	} /*else if (obj.value.length < 6 || obj.value.length > 16) {
		$("#errMsg").html("密码长度为6~16个字符");
		$("#tip-password").css("background", "");
	} else if (!obj.value.match(/^[a-zA-Z0-9]+$/)) {
		$("#errMsg").html("密码只能由字母和数字组成");
		$("#tip-password").css("background", "");
	}*/ else if (!obj.value.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/)) {
		$("#errMsg").html("必须包含大小写字母且长度为8-15位");
		$("#password_span").css('background', '');
		//$("#tip-password").css("background", "");
	} else {
		//		$("#errMsg").html("此密码可用");
		$("#true_password").val("1");
		//		$("#password_span_img").empty().append('<img src="'+themepath+'images/successicon.jpg"/>');
	}
}

//验证手机ajax交易
function telValidatePhone(obj, callback) {
	$.ajax({
		type: "POST",
		dataType: "JSON",
		url: basepath + "/user/isTelphoneBpCustMember.do?telphone=" + obj.value,
		cache: false,
		success: function (responseText, statusText) {
			callback && callback(responseText, statusText);
		},
		error: function (request) {
			return false;
		}
	});
}

var tel = "";
//个人注册手机号验证
function checkTelphone(obj) {
	$("#telphone_span_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
	tel = $("#telphone").val();
	if (isEmpty(tel)) {
		$("#errMsg").html("手机号码不能为空");
	} else if (isMobile(tel) == 0) {
		$("#errMsg").html("手机号码格式不对");
	} else {
		document.getElementById("telphone_span").style.color = "#999";
		//	    $("#errMsg").html("号码可以使用");
		$("#true_telphone").val("1");
		// $("#telphone_span_img").empty().append('<img width="18px"  src="'+themepath+'images/successicon.jpg"/>');
		/*telValidatePhone(obj, function(responseText, statusText){
			if (responseText.success) {
				if(responseText.result=="1"){
				   $("#errMsg").html(responseText.errMsg);
				}
			} else {
				if(responseText.result=="1"){
				    $("#errMsg").html(responseText.errMsg);
				}else{
				    $("#errMsg").html(responseText.errMsg);
				    $("#true_telphone").val("1");
				    $("#telphone_span_img").empty().append('<img width="18px"  src="'+themepath+'images/successicon.jpg"/>');
				}
			}
		});*/
	}

}



/**
 * 个人校验手机验证码
 * @param {} obj
 */
function validatTel2CheckCode(obj) {
	console.log("validatTel2CheckCode:" + obj);
	var tel = $("#tel_checkCode").val();
	if (isEmpty(tel)) {
		$("#telphone_span_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
		$("#errMsg").html("手机验证码不能为空");
		return false;
	} else {
		$(".form-group").show();
	}
}
//验证验证码
function validateGraphicCode(callback) {
	var graphicCode = $("#graphicCode").val();
	if (graphicCode.length != 4) {
		return false;
	}

	$.ajax({
		type: "POST",
		dataType: "JSON",
		url: basepath + "/user/isRightCheckCodeBpCustMember.do?checkCode=" + graphicCode,
		cache: false,
		success: function (responseText, statusText) {
			if (responseText.success) {
				$("#true_spancheckCode").val("1");
				$("errMsg").html("");
				callback && callback(responseText, true);
			} else {
				callback && callback(responseText, false);
			}
		},
		error: function (request) {
			callback && callback(request, false);
		}
	});
}

//点击验证码确定按钮
function confirmCode() {
	var telphone = $("#telphone").val();
	var password = $("#password").val();
	var graphicCode = $("#graphicCode").val();
	var tel_checkCode = $("#tel_checkCode").val();
	var errMsg = $("#errMsg");
	if (!telphone) {
		errMsg.text("手机号码不能为空");
		return false
	}
	if (isMobile(telphone)) {
		errMsg.text("手机号码格式不正确");
		return false
	}
	if (!password) {
		errMsg.text("密码不能为空");
		return false
	}
	if (!password.match(/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)[a-zA-Z\d]{8,}$/)) {
		errMsg.text("必须包含大小写字母且长度为8-15位");
		return false
	}
	if (!graphicCode || graphicCode.length() != 4) {
		errMsg.text("图形验证码错误！");
		return false
	}
	if (!tel_checkCode) {
		errMsg.text("手机验证码不能为空");
		return false
	}
	if (tel_checkCode.length() != 6) {
		errMsg.text("手机验证码不正确");
		return false
	}
	var code = $("#graphicCode").val(); //获取验证码的值
	if (isEmpty(code) || code.length != 4) {
		$("#graphicCode").focus();
		$("#errMsg").html("验证码错误！");
	} else {
		$("#errMsg").html("");
		validateGraphicCode(function (responseText, flag) {
			if (flag) { //验证图形验证码是否正确， 成功返回true，失败返回false
				//关闭弹框
				$(".verification").hide();
				//发送短信验证码
				//	if($(".type input[type='radio']:checked").val()=="personal"){
				getValidateSms("getVerifySms", "telphone", "verify_sms", 120, "bind_telphone");
				//} else {
				//	getValidateSms("busGetVerifySms", "busTelphone", "verify_sms", 120 , "bind_telphone");
				//}

			} else {
				$("#graphicCode").focus();
				$("#errMsg").html("验证码错误");
				refresh(document.getElementById("graphicCodeImg"));
			}
		});
	}
}

function getValidateSms(getVerifySmsId, mobileId, verifySmsId, seconds, type) {
	var selectType = $(".type input[type='radio']:checked").val();
	var topShow$;
	if (selectType == "personal") {
		topShow$ = $("#topShow");
	} else {
		topShow$ = $("#topShowBuss");
	}
	var temp = seconds;
	var getVerifySmsObj = $("#" + getVerifySmsId);
	var token = null;
	// 该按钮没有禁用
	if (!getVerifySmsObj.is(".disabled")) {
		// 禁用获取短信验证码按钮
		getVerifySmsObj.addClass("disabled");
		$("#" + verifySmsId).removeAttr("readonly");
		var text = getVerifySmsObj.html();
		//每次进入先清空
		getVerifySmsObj.html(text);
		var url = basepath + "codecreate.do?sms_code_type=" + type
			+ "&telphone=" + $("#" + mobileId).val() + '&randomCode='
			+ (Math.random()) + "&token=" + token + "&graphicCode=" + $("#graphicCode").val();
		console.log(url);
		$.ajax({
			url: url,
			data: { "memID": $("#memID").val() },
			dataType: "json",
			success: function (result) {
				if (result.status == "200") {
					//showTip("telphone","短信已发送至您手机，请输入短信中的验证码，30分钟内输入有效。");
					topShow$.html("短信已发送至您手机，请输入短信中的验证码，30分钟内输入有效。");
					$("#telphone").html("短信已发送至您手机，请输入短信中的验证码，30分钟内输入有效。");
					var interval = window.setInterval(function () {
						seconds--;

						if (seconds == 0) {
							seconds = temp;
							window.clearInterval(interval);
							getVerifySmsObj.removeClass("disabled");
							getVerifySmsObj.html(text.indexOf("重新") == -1
								? "重新" + text
								: text);
						} else {
							getVerifySmsObj.html("重新获取" + "(" + seconds + ")");
						}
					}, 1000);
				} else {
					//									document.getElementById("phone_msg").style.color = "red";
					topShow$.html(result.remark);
					getVerifySmsObj.removeClass("disabled");
					getVerifySmsObj.html(text.indexOf("重新") == -1 ? "重新" + text : text);

				};
			},
			error: function () {
				//							$("#showMsg").parent().css("display","block");
				//							$("#showMsg").html("短信验证码获取失败，请重新获取");
				//showTip("telphone","短信验证码获取失败，请重新获取");
				topShow$.html("短信验证码获取失败，请重新获取");
			}
		});

	} else {
		alert("发送验证码不能点击");
		return false;
	}
}





/**
 * 个人注册手机验证码是否正确
 * @param {} obj
 */
function checkTelphoneCode(obj) {
	$("#phone_msg_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
	var telCode = $("#tel_checkCode").val();
	var getVerifySmsObj = $("#getVerifySms");
	if (isEmpty(telCode)) {
		$("#errMsg").html("手机验证码不能为空");
	} else {
		$.ajax({
			type: "POST",
			dataType: "JSON",
			url: basepath + "/user/checkPhoneCodereg.do?telphone=" + tel + "&checkCode=" + obj.value,
			cache: false,
			success: function (responseText, statusText) {
				if (responseText.success) {
					if (responseText.result == "2") {
						$("#errMsg").html(responseText.Msg);
					} else {
						$("#true_phone_msg").val("1");
						$("#errMsg").html("");
					}
				} else {
					if (responseText.result == "1") {
						$("#errMsg").html(responseText.Msg);
					}
				}
			},
			error: function (request) {
				return false;
			}
		});
	}
}


function checkisExistRecommand(obj) {
	var recommandPerson = $("#recommendP_info").val();
	if (recommandPerson == "") {
		$("#true_recommandPerson_msg").val("");
	} else {
		$.ajax({
			type: "POST",
			dataType: "JSON",
			url: basepath + "/user/isExistRecommandBpCustMember.do?recommandPerson=" + recommandPerson,
			cache: false,
			success: function (responseText, statusText) {
				if (responseText.success) {
					$("#errMsg").html("邀请码有效");
					$("#true_recommandPerson_msg").val("");
					$("#recommandPerson_msg_img").empty().append('<img width="18px"  src="' + themepath + 'images/successicon.jpg"/>');
				} else {
					$("#errMsg").html("不存在此邀请码");
					$("#true_recommandPerson_msg").val("0");
					$("#recommandPerson_msg_img").empty().append('<img width="18px"  src="' + themepath + 'images/errorimg.png"/>');
				}
			},
			error: function (request) {
				return false;
			}
		});
	}
}
